package com.yisu.expressway.shopping_mall;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.yisu.expressway.R;
import com.yisu.expressway.adapter.ShoppingMallTabAdapter;
import com.yisu.expressway.fragment.BaseFragment;
import com.yisu.expressway.model.ShopCategory;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/9/29 0029.
 */
public class ShoppingMallFragment extends BaseFragment {
    private View parentView;

    private ShoppingMallTabAdapter mAdapter;

    private ArrayList<ShopCategory> mCategoryList;

    @Bind(R.id.tab_layout)
    protected TabLayout mNaviTabLayout;
    @Bind(R.id.viewpager)
    protected ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(parentView == null) {
            parentView = inflater.inflate(R.layout.fragment_shopping_mall, container, false);
        }

        ViewGroup rootView = (ViewGroup) parentView.getParent();
        if (rootView != null) {
            rootView.removeView(parentView);
        }

        ButterKnife.bind(this,parentView);
        getShopCategory();

        return parentView;
    }

    private void getShopCategory(){
        showLoading();

        ApiClientHelper.getApi(ApiSetting.getShopCategory(), new TypeToken<ArrayList<ShopCategory>>() {
                },
                new Response.Listener<ApiResult<ArrayList<ShopCategory>>>() {
                    @Override
                    public void onResponse(final ApiResult<ArrayList<ShopCategory>> result) {

                        dismissLoading();

                        if (!result.success) {
                            return;
                        }

                        mCategoryList = result.getResult();
                        if(mCategoryList != null && mCategoryList.size() > 0) {
                            initTab();
                        }

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        dismissLoading();
                    }
                }, this);

    }

    private void initTab(){
        if(mCategoryList.size() > 5){
            mNaviTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        }
        mAdapter = new ShoppingMallTabAdapter(this,mCategoryList);
        mViewPager.setAdapter(mAdapter);
        mNaviTabLayout.setupWithViewPager(mViewPager);
        for (int i = 0; i < mCategoryList.size(); i++) {
            TabLayout.Tab tab = mNaviTabLayout.getTabAt(i);
            if (tab != null) {
                tab.setText(mCategoryList.get(i).categoryName);
            }
        }
        mViewPager.setCurrentItem(0,false);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
